import { message } from 'antd';
import { Component } from 'react';
import { findDOMNode } from 'react-dom';

let ipt = document.createElement('textarea');
ipt.setAttribute(
  'style',
  'height:4px;width:4px;font-size:12px;transform:scale(0.01);border:0 none;position:absolute;opacity:0;top:0;left:0;z-index:-1',
);
document.body.appendChild(ipt);

export default class Clip extends Component {
  static copy(value, msg = 'Copy Success！') {
    ipt.value = value;
    ipt.select();
    document.execCommand('copy', false);
    message.success(msg);
  }

  componentDidMount() {
    let dom = findDOMNode(this);
    dom.addEventListener('click', () => {
      ipt.value = this.props.value;
      ipt.select();
      document.execCommand('copy', false);
      message.success(this.props.message || 'Copy Success！');
      this.props.onCopy && this.props.onCopy();
    });
  }
  render() {
    const { children } = this.props;
    return children;
  }
}
